package com.ifeng.igps.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.ifeng.igps.R;
import com.ifeng.igps.action.Action;
import com.ifeng.igps.constant.Constant;
import com.ifeng.mu.widget.MU_Title_Style1;
import com.ifeng.mu.widget.MU_Toast;

public class ActSettings extends Activity {
	private MU_Title_Style1 title;
	private Button btnEdit;
	private Button btnUploadAll;
	private UploadAllReceiver uploadAllRece;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_settings);

		init();
	}

	private void init() {
		title = (MU_Title_Style1) findViewById(R.id.act_settings_title);
		btnEdit = (Button) findViewById(R.id.act_settings_btn_edit);
		btnUploadAll = (Button) findViewById(R.id.act_settings_btn_upload_all);
		uploadAllRece = new UploadAllReceiver();
		title.init("Settings", true, "Home", false, null);
		title.setLeftBtnOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(ActSettings.this, ActHome.class);
				startActivity(i);
				ActSettings.this.finish();
			}
		});

		btnEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(ActSettings.this, ActFitnessInfo.class);
				i.putExtra("pageOption", 1);
				startActivity(i);
			}
		});
		btnUploadAll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new Action(ActSettings.this).uploadAllRecords();
			}
		});
	}

	class UploadAllReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// String result = intent.getStringExtra("result");
			// if (result == Constant.SUCESS) {
			// new MU_Toast(ActSettings.this)
			// .showBottomShortToast("Upload All Records succeeded~");
			// ActSettings.this.finish();
			// }
			new MU_Toast(ActSettings.this)
					.showBottomShortToast("This function is not perfect, \nplease pay attention to the software update~");
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		LocalBroadcastManager.getInstance(this).registerReceiver(uploadAllRece,
				new IntentFilter(Constant.IGPS_UPLOADALL_RESULT));
	}

	@Override
	protected void onStop() {
		super.onStop();
		LocalBroadcastManager.getInstance(this).unregisterReceiver(
				uploadAllRece);
	}
}
